<html>
<head><meta charset="utf-8"><title>Questions about Clippy · clippy · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/index.html">clippy</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Questions.20about.20Clippy.html">Questions about Clippy</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220364994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Questions%20about%20Clippy/near/220364994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> José Duarte <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Questions.20about.20Clippy.html#220364994">(Dec 18 2020 at 12:16)</a>:</h4>
<p>Hello everyone! Let me preface by saying that I'm new to Zulip and I am sorry if I sent this message to the wrong topic or broke a rule related to message sending.</p>
<p>I'm here to make some questions regarding clippy, for context I'm currently working on my thesis, about Typestates in Rust.<br>
I've developed a small limited macro prototype (<a href="https://github.com/rustype/typestate-rs">https://github.com/rustype/typestate-rs</a>) and now I'm exploring the ecosystem for other possible alternatives.</p>
<p>In a super small nutshell what I want to achieve is enabling the user to write some code like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[to=b]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">a</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="cp">#[from=a, to=c]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">b</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="cp">#[from=b]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">c</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">..</span><span class="p">.</span><span class="w"> </span><span class="p">}</span><span class="w"></span>

<span class="n">a</span><span class="p">();</span><span class="w"> </span><span class="n">b</span><span class="p">();</span><span class="w"> </span><span class="n">c</span><span class="p">();</span><span class="w"> </span><span class="c1">// ok</span>
<span class="n">b</span><span class="p">();</span><span class="w"> </span><span class="n">a</span><span class="p">();</span><span class="w"> </span><span class="n">c</span><span class="p">();</span><span class="w"> </span><span class="c1">// error: protocol dictates b comes from a</span>
</code></pre></div>
<p>The code above is a REALLY rough draft. I'm aware that I can do something like it with types (I've written about it here <a href="https://rustype.github.io/notes/notes/rust-typestate-series/rust-typestate-index">https://rustype.github.io/notes/notes/rust-typestate-series/rust-typestate-index</a>)<br>
But types imply some limitations, such as not being able to use a <code>Vec&lt;Drone&lt;_&gt;&gt;</code>; while producing errors with clippy seems to be a possible solution.</p>
<p>Finally, the questions:</p>
<ul>
<li>Can clippy be extended with external lints (i.e. not present in the clippy repo)?</li>
<li>Can clippy "read" the AST before macro expansion? This would enable something like the example above, where I need to establish a graph and check if A -&gt; B -&gt; C </li>
<li>If clippy is not able to do any of the above, is there any way that it would be possible, without writing a whole static analyzer from scratch? Like hooking into <code>rustc</code> and getting direct access to the AST</li>
</ul>



<a name="220366884"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Questions%20about%20Clippy/near/220366884" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Questions.20about.20Clippy.html#220366884">(Dec 18 2020 at 12:40)</a>:</h4>
<p>You 3rd point is correct: You have to hook into rustc if you  want to do static analysis.  This is quite easy, though. Clippy does this in <code>src/driver.rs</code>. Don't get scared by the file size, most of it is boilerplate for  getting things to work with lint level handling and such. Take a look at the <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_driver/struct.RunCompiler.html"><code>RunCompiler</code></a> struct. In the callbacks part you can  register lints, which will then emit errors.</p>
<p>To read the state of the AST before macro expansion, there currently is <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint/context/struct.LintStore.html#method.register_pre_expansion_pass"><code>pre_expansion_pass</code></a> . But this only exists, because Clippy still uses it. But probably will be deprecated once Clippy rewrites all the pre_expansion_pass lints.</p>



<a name="220368706"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Questions%20about%20Clippy/near/220368706" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> José Duarte <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Questions.20about.20Clippy.html#220368706">(Dec 18 2020 at 13:01)</a>:</h4>
<p>Thank you for the answer, do you know where I can get more information on <code>rustc</code> and its passes etc? Is there an "architectural" book?<br>
Furthermore would hooking into the compiler be considered an "hack"? Finally, rust supression attributes like <code>#[allow(...)]</code> are not actually macros correct? Are they erased from the AST in these passes?</p>



<a name="220369126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Questions%20about%20Clippy/near/220369126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Questions.20about.20Clippy.html#220369126">(Dec 18 2020 at 13:05)</a>:</h4>
<p>Take a look here: <a href="https://rustc-dev-guide.rust-lang.org/diagnostics.html">https://rustc-dev-guide.rust-lang.org/diagnostics.html</a></p>
<p>The rustc-dev-guide is a pretty good place to find out more about the compiler.</p>
<p>No the suppression attributes are parsed and handled by the rust compiler, without expanding them. What you want to do looks like you want to use <code>proc_macro</code>s. For example the <code>#[derive]</code> attribute is a <code>proc_macro</code> which generates code for the item it is attached to.</p>



<a name="220369498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Questions%20about%20Clippy/near/220369498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> José Duarte <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Questions.20about.20Clippy.html#220369498">(Dec 18 2020 at 13:09)</a>:</h4>
<p>I'm aware of the <code>proc_macro</code>s, the original idea was to use them, but since they can't relate bigger pieces of code they wouldn't work.<br>
The question was more like "can i create pseudo attribute macros like <code>allow</code> and after analysis clean them up, as if they stay, they're not macros and thus wouldn't compile</p>



<a name="220369681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Questions%20about%20Clippy/near/220369681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Questions.20about.20Clippy.html#220369681">(Dec 18 2020 at 13:11)</a>:</h4>
<p>I don't think you can create pseudo attributes without modifying the compiler directly.</p>



<a name="220369871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Questions%20about%20Clippy/near/220369871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> José Duarte <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Questions.20about.20Clippy.html#220369871">(Dec 18 2020 at 13:12)</a>:</h4>
<p>Then how does clippy accomplish the <code>#![warn(clippy::wildcard_dependencies)]</code> for example?</p>



<a name="220370161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Questions%20about%20Clippy/near/220370161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> flip1995 <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Questions.20about.20Clippy.html#220370161">(Dec 18 2020 at 13:15)</a>:</h4>
<p>rustc has code for that specific attribute. See: <a href="https://github.com/rust-lang/rust/blob/d8d3ab96aa94423f1bdfbb605098a506ab5edc7e/compiler/rustc_lint/src/levels.rs#L156-L175">https://github.com/rust-lang/rust/blob/d8d3ab96aa94423f1bdfbb605098a506ab5edc7e/compiler/rustc_lint/src/levels.rs#L156-L175</a></p>



<a name="220370329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/257328-clippy/topic/Questions%20about%20Clippy/near/220370329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> José Duarte <a href="https://rust-lang.github.io/zulip_archive/stream/257328-clippy/topic/Questions.20about.20Clippy.html#220370329">(Dec 18 2020 at 13:16)</a>:</h4>
<p>I see. Thank you for all the help!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>